package com.aletto.general_store.ui.adapters;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.aletto.general_store.R;
import com.aletto.general_store.data.model.OrderItem;
import com.aletto.general_store.data.model.ServiceOrder;
import com.aletto.general_store.presenters.OrderRaiseArbitrationTask;
import com.aletto.general_store.presenters.OrderResolveArbitrationTask;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.math.BigDecimal;
import java.util.List;

public class AppealOrderAdapter extends BaseMultiItemQuickAdapter<ServiceOrder, BaseViewHolder> {

    public AppealOrderAdapter() {
        addItemType(ServiceOrder.ONGOING, R.layout.item_appeal_order_ongoing); // 进行中
        addItemType(ServiceOrder.COMPLETED, R.layout.item_appeal_order_completed); // 已完成
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ServiceOrder item) {
        OrderItem.ShopDTO shop = item.getShop();

        BigDecimal price = new BigDecimal(item.getPrice()).multiply(new BigDecimal(item.getNum()));

        holder.setText(R.id.tv_goods_title, item.getGoods_name())
                .setText(R.id.tv_sold_number, "× " + item.getNum())
                .setText(R.id.tv_goods_price, "实付：" + XHelper.toPrice(price) + " USDT");

        // 商品图片
        List<String> goodsImg = item.getGoods_img();
        if (goodsImg != null && !goodsImg.isEmpty()) {
            ImageView iv_goods_cover = holder.getView(R.id.iv_goods_cover);
            GlideApp.with(iv_goods_cover).load(goodsImg.get(0)).normal().into(iv_goods_cover);
        }

        switch (item.getItemType()) {
            case ServiceOrder.ONGOING:
                View order_refund_agree = holder.getView(R.id.tv_logistics);
                View order_refund_refuse = holder.getView(R.id.tv_confirm);

                order_refund_agree.setOnClickListener(v -> // 同意退款
                        new OrderResolveArbitrationTask(item.getBtc_order_id(), false).execute());

                order_refund_refuse.setOnClickListener(v -> // 拒绝退款
                        new OrderResolveArbitrationTask(item.getBtc_order_id(), true).execute());
                break;

            case ServiceOrder.COMPLETED:
                holder.setText(R.id.tv_order_status, item.getStatus() == 10 ? "拒绝退款" : "同意退款");
                break;
        }
    }

}
